package models;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.*;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import play.data.validation.*;
import play.db.jpa.Model;
import play.libs.Mail;

/**
 * Email Model to allow the system to send notifications to the users automatically 
 * when the method is called.
 * 
 * @author Mohamed Abdel Kader
 */


@Entity
public class Email extends Model {

	/**An attribute source which holds the source of the email and must
	 * be in the form of example@example.com and it is required
	 * @author Mohamed Abdel Kader*/
	@Required
	public String source;

	/**An attribute destination which holds the destination of the email and must
	  be in the form of example@example.com* and it is required
	  @author Mohamed Abdel Kader*/
	
	@Required
	public String destination;

	
	/**An attribute subject which holds the subject of the email
	 * @author Mohamed Abdel Kader*/
	public String subject;

	
	/**An attribute message which holds the body of the email
	 * @Lob to tell the the database this field might store
	 * large number of characters 
	 * @author Mohamed Abdel Kader*/
	@Lob
	public String message;

	
	/**
	 * A constructor which take the message source, destination, subject
	 * and the body which is the message and creates a new Email with these parameters
	 * @param Source for the email, Destination for the email, subject of the email and message 
	 * the body of the email*
	 * @author Mohamed Abdel Kader */
	
	public Email(String source, String destination, String subject,
			String message) {
		this.source = source;
		this.destination = destination;
		this.subject = subject;
		this.message = message;
	}

	
	
	/**sendEmail() method creates a simple email which is from play 
	library sets the source, destination, subject and message
	taken from attributes of the email model then send it
	
	@author Mohamed Abdel Kader*/

	public void sendEmail() throws EmailException {

		SimpleEmail email = new SimpleEmail();
		email.setFrom("[Ideas Hub] " + source);
		email.addTo(destination);
		email.setSubject(subject);
		email.setMsg(message);
		Mail.send(email);

	}

}
